package com.leyou.cart.controller;

import com.leyou.cart.pojo.Cart;
import com.leyou.cart.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    //登录状态下添加购物车
    @PostMapping
    public ResponseEntity<Void> addCart(@RequestBody List<Cart> carts) {  //接收的对象是Json格式的
        this.cartService.addCart(carts);
        return ResponseEntity.ok().build();
    }

    //登录状态下查询购物车
    @GetMapping
    public ResponseEntity<List<Cart>> queryCarts() {
        //查询全部数据
        List<Cart> list = this.cartService.queryCarts();
        if (null == list || list.size() < 0) {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
        return ResponseEntity.ok(list);
    }

    //登录状态下修改商品数量
    @PutMapping
    public ResponseEntity<Void> updateCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        //修改商品数量
        this.cartService.updateCart(skuId,num);
        return  ResponseEntity.ok().build();
    }

    //登录状态下删除商品
    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") String skuId) {
        //删除商品
        this.cartService.deleteCart(skuId);
        return ResponseEntity.ok().build();
    }

}
